define(function(require, exports, module) {

	var cookieSetting = require("cookieSetting");
	var layer = require("layer");
	var common = require("common");
	var speed = 100;
	var defaults = {
		//
		beforeClick: function() {
			return true;
		}, //单击之前回调
		itemClick: function() {} //点击每项回调
	};

	var init = function() {

		var menu = $(".page_side_menu");
		menu.find("li a").mouseup(function() {
			toggleItem($(this));
		});

		var userSetting = cookieSetting.getSetting();
		//设置选择带单
		menu.find("li[id='" + userSetting.activeId + "']").addClass(function() {
			$(this).addClass("active");
			if ($(this).parent("ul.submenu")) {
				$(this).parent("ul.submenu").parent("li").addClass("active");
			}
		});
		menu.find("li[id='" + userSetting.openId + "']").addClass("open");
		// 设置菜单是否最小化
		setMenu(userSetting.mini);
		$(".mini-icon").click(function() {
			setMenu(!userSetting.mini);
			userSetting.mini = !userSetting.mini;
			cookieSetting.saveSetting(userSetting);
		});
		menu.find("li a[data-ajax='true']").click(function() {
			return false;
		});

		/*$("#page-sidebar-collapse").click(function () {
			if ($("#page-sidebar").is(":hidden")) {
				$("#page-sidebar-collapse").collapse('show');
			} else {
				$("#page-sidebar-collapse").collapse('hide');
			}
		});*/
	}

	var setMenu = function(mini) {
		if (mini) {
			$(".page_sidebar").addClass("menu-min");
			$(".main_content").addClass("menu-min");
			$("#sidebar-collapse > i").attr("class", $("#sidebar-collapse > i").attr("data-icon2"));
		} else {
			$(".page_sidebar").removeClass("menu-min");
			$(".main_content").removeClass("menu-min");
			$("#sidebar-collapse > i").attr("class", $("#sidebar-collapse > i").attr("data-icon1"));
		}
	}

	//显示或隐藏下级菜单
	var toggleItem = function(item) {
		var userSetting = cookieSetting.getSetting();
		var parent = item.parent("li");
		var submenu = parent.find("ul.submenu");
		if (submenu[0]) {
			if (parent.hasClass("open")) {
				submenu.slideUp(speed, function() {
					parent.removeClass("open");
				});
				userSetting.openId = "";
			} else {
				/*var hasSelectedItem = submenu.find('li.active').length > 0;
				if (hasSelectedItem) {
					parent.removeClass("active");
				}*/
				parent.parent(".page_side_menu").find("li.open > ul.submenu").hide();
				parent.parent(".page_side_menu").find("li.open").removeClass("open");
				submenu.slideDown(speed, function() {
					parent.addClass("open");
					/*if (hasSelectedItem) {
						parent.addClass("active");
					}*/
				});
				userSetting.openId = parent.attr("id");
			}
		} else {
			$(".page_side_menu li.active").removeClass("active");
			parent.addClass("active"); //选中点击的a标签  同时选中 根标签
			if (parent.parent("ul.submenu")) {
				parent.parent("ul.submenu").parent("li").addClass("active");
			}
			userSetting.activeId = parent.attr("id");
		}
		cookieSetting.saveSetting(userSetting);
		if (typeof(item.attr("data-ajax")) != "undefined") {
			var beforeClick = defaults.beforeClick(item);
			if (beforeClick) {
				var href = item.attr("href");
//				var reg = /javascript|[^\w]/
//				if (href && reg.test(href)) { //判断是否合法
					//var index = layer.load(0, {shade: [0.4,'#fafafa']}); //打开loading
//					common.showProcess();
//					$(".main_content").hide();
//					$(".main_content").load(href, function(response, status, xhr) { //ajax页面加载
						//layer.close(index); //关闭loading
//						common.closeProcess();
//						cookieSetting.saveSetting(userSetting);
//						if (userSetting.breadcrumb) {
//							$(".breadcrumb").addClass("breadcrumb_fixed");
//						}
//						$(".main_content").show();
//					});
//				}
				common.ajaxLoadPage(href);
			}
			defaults.itemClick(item); //点击回调
			if (!$("#page-sidebar-collapse").is(":hidden")) {
				$("#page-sidebar").collapse('hide');
			//	$("#page-sidebar-collapse").collapse('toggle');
			}
		}
	}

	return {
		init: init
	}
});